@extends('admin/layouts/admin')
@section('title', empty($data) ? '添加广告' : '编辑广告')
@section('main')
<div class="main-title"><h2>@if (!empty($data)) 编辑@else添加@endif广告</h2></div>
<div class="main-section">
  <div style="width: 543px">
    <form action="{{ url('/advcontent/save') }}" method="post" class="j-form">
      <div class="form-group row">
        <label class="col-sm-3 col-form-label">选择广告位</label>
        <div class="col-sm-9">
          <select name="advid" class="form-control" style="width:200px">
            <option value="0">---</option>
            @foreach ($position as $v)
            <option value="{{ $v['id'] }}" @if ($v['id'] == $data->advid) selected @endif>
              {{ $v['name'] }}
            </option>
            @endforeach
          </select>
        </div>
      </div>
      <div class="form-group row">
        <label class="col-sm-3 col-form-label">上传图片</label>
        <div class="col-sm-9">
          <input type="file" id="file1" value="上传图片">
          <div class="upload-img-box" id="uploadImg">
            @if (isset($data->path))
            <div class="upload-pre-item" style="min-height:100%">
              @foreach ($data->path as $val)
              <img src="/static/upload/{{$val}}" alt="" style="width:100px;height:100px">
              <input type="hidden" name="path[]" value="{{$val}}" class="icon_banner">
              @endforeach
            </div>
            @endif
          </div>
        </div>
      </div>
      <div class="form-group row">
        <div class="col-sm-10">
          {{ csrf_field() }}
          @if (isset($data['id']))
          <input type="hidden" name="id" value="{{$data->id}}">
          @endif
          <button type="submit" class="btn btn-primary mr-2">提交表单</button>
          <a href="{{ url('advcontent') }}" class="btn btn-secondary">返回列表</a>
        </div>
      </div>
    </form>
  </div>
</div>
<script src="{{asset('admin')}}/common/uploader/jquery.uploadifive.js"></script>
<script>
  main.menuActive('advcontent');
  $('head').append('<link href="{{asset('admin')}}/common/uploader/uploadifive.css" rel="stylesheet">');
  $('#file1').uploadifive({
    auto: true,
    fileObjName: 'image',
    fileType: 'image',
    buttonText: '上传图片',
    formData: { _token: '{{ csrf_token() }}' },
    method: 'post',
    queueID: 'uploadImg',
    removeCompleted: true,
    uploadScript: '{{ url('advcontent/upload') }}',
    onUploadComplete: uploadPicture
  });
  function uploadPicture(file, data) {
    var obj = $.parseJSON(data);
    if (obj.code) {
      var filename = obj.data.filename;
      var path = obj.data.path;
      var img = '<img src="' + path + '" style="width:100px;height:100px"><input type="hidden" name="path[]" value="' + filename + '" class="icon_banner">';
      if ($('.upload-pre-item').length > 0) {
        $('.upload-pre-item').append(img);
      } else {
        $('.upload-img-box').html(
          '<div class="upload-pre-item" style="min-height:100%;">' + img + '</div>'
        );
      }
    } else if (data.info) {
      alert(data.info);
    } else {
      alert(data);
    }
  }
</script>
@endsection